<template>
	<view class="service">
		<view class="tabs">
			<view class="box">
				<scroll-view scroll-x style="white-space: nowrap;" :scroll-into-view="'tab-' + activeTabIndex"
					scroll-with-animation>
					<image :id="'tab-' + i" v-for="(e,i) in tabsList" :key="i" @click="tabsClick(e,i)"
						:src="active == i ? e.img : e.pic" :alt="e.cate_name" />
				</scroll-view>
			</view>
		</view>

		<view class="list">
			<uv-text text="精选服务" size="34upx" color="#333333" />
			<uv-gap height="10" bgColor="#F6F4F7"></uv-gap>
			<view v-for="(item,index) in list" :key="index" @click="toRoute('/pages-home/service/detail?id='+item.id)">
				<view class="item flex">
					<uv-image :src="item.image" width="70px" height="70px" />
					<view class="info w-full">
						<view class="title">{{item.store_name}}</view>
						<view class="make">免费预约</view>
						<view class="flex">
							<text class="cost">已售{{item.sales}}</text>
							<text class="cost" style="margin-left: 30upx;">好评率96%</text>
						</view>
					</view>
				</view>
				<uv-gap height="30upx" bgColor="#F6F4F7"></uv-gap>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		getIconPath
	} from '@/util'
	import {
		getCategory,
		getListByCate
	} from '@/api'
	import {
		onLoad,
		onReachBottom
	} from '@dcloudio/uni-app'

	const active = ref(0)
	const activeTabIndex = ref(0); // 控制滚动位置
	const tabsList = ref([])
	const page = ref(1)
	const list = ref([])
	const total = ref(0)
	const finished = ref(false)
	const category_id = ref(0)
	const cate_id = ref(0)
	const toRoute = (url) => uni.navigateTo({
		url
	})
	const tabsClick = (e, i) => {
		active.value = i
		activeTabIndex.value = i; // 触发滚动到对应元素
		cate_id.value = e.id
		page.value = 1
		list.value = []
		getList()
	}
	const getList = async () => {
		finished.value = false;
		if (page.value === 1) list.value = []
		const {
			result
		} = await getListByCate({
			page: page.value,
			limit: 10,
			category_id: category_id.value,
			cate_id: cate_id.value
		})
		page.value++
		total.value = result.total
		list.value = [...list.value, ...result.data]
		finished.value = list.value.length >= result.total;
	}
	onReachBottom(() => {
		if (finished.value) return
		getList()
	})
	onLoad((option) => {
		const init = async () => {
			category_id.value = option.id
			const {
				result
			} = await getCategory({
				category_id: option.id
			})
			tabsList.value = result
			tabsList.value.unshift({
				"id": 0,
				"cate_name": "热门服务",
				"img": 'https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/bc1fd202508161117032231.png',
				"pic": 'https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/b8dde20250816111622253.png'
			})
		}
		init()
		getList()
		uni.setNavigationBarTitle({
			title: decodeURIComponent(option.cate)
		})
	})
</script>

<style>
	page {
		background-color: #F6F4F7;
	}
</style>

<style lang="scss" scoped>
	.tabs {
		top: var(--window-top);
		z-index: 10;
		position: sticky;
		background-color: #fff;

		.box {
			margin: 0 30upx;
			overflow: hidden;
		}

		image {
			width: 160upx;
			height: 160upx;
			display: inline-block;
		}
	}

	.list {
		margin: 30upx;

		.item {
			padding: 30upx;
			border-radius: 10upx;
			box-sizing: border-box;
			background-color: #fff;

			.info {
				display: flex;
				margin-left: 30upx;
				flex-direction: column;
				justify-content: space-between;

				.title {
					color: #333333;
					font-size: 30upx;
					font-weight: 700;
				}

				.make {
					color: #E59D37;
					font-size: 28upx;
					font-weight: 400;
				}

				.cost {
					color: #C3C3C5;
					font-size: 20upx;
				}
			}
		}
	}
</style>